package array

func FindTheWinner(n int, k int) int {
	player := []int{0}
	// 构建玩家
	for i := 1; i <= n; i++ {
		player = append(player, i)
	}

	out := []int{}

	index := 1

	for len(out) != n-1 {
		step := 1
		for step < k {
			index++
			if index > n {
				index = 1
			}
			if player[index] != -1 {
				step++
			}
		}

		val := player[index]
		out = append(out, val)
		player[index] = -1

		index++
		if index > n {
			index = 1
		}
		for player[index] == -1 {
			index++
			if index > n {
				index = 1
			}
		}
	}

	for i := 1; i <= n; i++ {
		if player[i] != -1 {
			return player[i]
		}
	}

	return -1

}
